﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SysFrame.BP;
using SysFrame.Message;

namespace AppServer.Controllers.System
{
    [Route("api/[controller]")]
    [ApiController]
    public class System4Controller : SysControllerBase
    {
        //字典
        // POST: api/System6
        [HttpPost]
        public ActionResult<string> Post([FromBody] dynamic data)
        {
            string jsTimeStamp = data.jsTimeStamp;
            string Action = data.Action == null ? "Get" : data.Action;
            string rtstr = "";
            if (Action == "Get")
            {
                rtstr = Get(data);
            }
            if (Action == "GetSys4ByCode")
            {
                rtstr = GetSys4ByCode(data);
            }
            if (Action == "GetSys4ByTypeCode")
            {
                rtstr = GetSys4ByTypeCode(data);
            }
            if (Action == "GetDDLitemBySys5code")
            {
                rtstr = GetDDLitemBySys5code(data);
            }
            if (Action == "Save")
            {
                rtstr = Save(data);
            }
            if (Action == "SaveByCode")
            {
                rtstr = SaveByCode(data);
            }
            return rtstr;
        }

        /// <summary>
        /// 得到字典
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Get(dynamic data) {
            //分类id
            JObject postdata = JObject.FromObject(data.data);
            string rowid = postdata["rowid"].ToString();
            MsQuery ms = new MsQuery();
            //条件
            if (true)
            {
                ms.strquery += " and 1=1 ";
                ms.strquery += " and SYSTEM_4_30='" + rowid + "' ";
            }
            //ms.strorder = "  ";//排序语句
            List<Mssystem_4> listms = new BLLsystem_4().Getsystem_4MsByQuery(ms);
            return JArray.FromObject(listms).ToString();

        }

        //得到字典 通过字典分类编号
        private string GetSys4ByTypeCode(dynamic data)
        {
            //分类id
            JObject postdata = JObject.FromObject(data.data);
            string code = postdata["typecode"].ToString();
            List<Mssystem_4> listmssys4 = new BLLsystem_4().Getsystem_4ByTypeCode(code);
            return JArray.FromObject(listmssys4).ToString();
        }
        //得到字典 通过字典编号
        private string GetSys4ByCode(dynamic data)
        {
            //分类id
            JObject postdata = JObject.FromObject(data.data);
            string code = postdata["code"].ToString();
            Mssystem_4 sys4 = new BLLsystem_4().Getsystem_4ByCode(code);
            return JObject.FromObject(sys4).ToString();
        }

        //得到下来by分类code
        private string GetDDLitemBySys5code(dynamic data)
        {
            //分类id
            JObject postdata = JObject.FromObject(data.data);
            string typecode = postdata["code"].ToString();
            List<Mssystem_4> listsys4 = new BLLsystem_4().Getsystem_4ByTypeCode(typecode);
            JArray listitem = new JArray();
            foreach (var item in listsys4)
            {
                JObject i = new JObject();
                i.Add("value",item.System_4_20);
                i.Add("text", item.System_4_40);
                listitem.Add(i);
            }
            return listitem.ToString() ;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Save(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            Mssystem_4 ms = Mssystem_4.parse<Mssystem_4>(postdata);
            //ms8.System_8_50 = "1";
            MsReturned rt = new BLLsystem_4().Savesystem_4(ms);
            return JObject.FromObject(rt).ToString();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string SaveByCode(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string typecode = postdata["code"].ToString();
            string name = postdata["name"].ToString();
            string value1 = postdata["value1"].ToString();
            string value2 = postdata["value2"].ToString();
            //ms8.System_8_50 = "1";
            MsReturned rt = new BLLsystem_4().Savesystem_4(typecode,name,value1,value2);
            return JObject.FromObject(rt).ToString();
        }
    }
}